﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace FlexControls
{
    
    public partial class fcTextBox : UserControl
    {
        public bool booLoadList = true;
        public ADODB.Connection txtConn;
        public string strProc_ListView="";
        public new event EventHandler DoubleClick;
        public string txtCode = "";
        protected virtual void OnDoubleClick()
        {
            if (this.DoubleClick != null)
            {
                this.DoubleClick(this, EventArgs.Empty);

            }
        }
        public fcTextBox()
        {
            InitializeComponent();
            this.Width = 245;
            this.Height = 22;
            textBox.MaximumSize=new System.Drawing.Size(600, 20);
            textBox.MinimumSize= new System.Drawing.Size(30, 20);
            textBox.Location.X.Equals(1);
            textBox.Location.Y.Equals(1);

        }

        public string fcProc_ListView
        {
            get
            {
                return strProc_ListView;
            }
            set
            {
                strProc_ListView = value;
            }
        }
        public bool booReadOnly=false;
        public bool fcReadOnly
        {
            get
            {
                return booReadOnly;
            }
            set
            {
                booReadOnly = value;
                if (booReadOnly == true)
                {
                    textBox.BackColor = System.Drawing.Color.LightGoldenrodYellow;
                    booGenCode = false;
                    textBox.ReadOnly = true;
                }
                else if (booGenCode == false && booReadOnly == false && booDate==false)
                {

                    textBox.BackColor = System.Drawing.Color.White;
                    textBox.ReadOnly = false;
                }
            }
        }

        private void fcTextBox_SizeChanged(object sender, EventArgs e)
        {
            this.Height = 22;
            textBox.Width = this.Width - 2;
            textBox.Location.X.Equals(1);
            textBox.Location.Y.Equals(1);
            dateTimePicker.Width = this.Width - 2;
            dateTimePicker.Location=new Point(1,Convert.ToInt32(dateTimePicker.Location.Y));
        }
        public bool booGenCode = false;
        public bool fcGenCode
        {
            get
            {
                return booGenCode;
            }
            set
            {
                booGenCode = value;
                if (booGenCode == true)
                {
                    booReadOnly = false;
                    textBox.BackColor = System.Drawing.Color.LightSkyBlue;
                    textBox.ReadOnly = true;
                }
                else if (booReadOnly == false && booGenCode == false && booDate==false)
                {
                    textBox.BackColor = System.Drawing.Color.White;
                    textBox.ReadOnly = false;

                }
            }
        }
        
        public string strCode="";
        public string fcCode
        {
            get
            {
                return strCode;
            }
            set
            {
                strCode = value;
            }
        }
        public string strText = "";
        public string fcText
        {
            get
            {
                return strText;
            }
            set
            {
                strText = value;
                if (booDate == false)
                {
                    if (strText != "")
                    {
                        if (strText == " ")
                        {
                            strText = "";
                            textBox.Text = "";
                        }
                        else
                            textBox.Text = strText;
                    }
                    else if (strText == "")
                    {
                        textBox.Text = strText;
                    }
                }
                else 
                {
                    if (strText != "")
                    {

                        
                        char[] delimiters = new char[] { ' '};
                        string[] strCode = strText.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        //DateTime a = DateTime.Parse(strCode[0], CultureInfo.CreateSpecificCulture("fr-FR"));

                        DateTime a = DateTime.Parse(strCode[0]);
                        dateTimePicker.Value = a;
                        textBox.Text = strText;
                    }
                    
                }
            }
        }


        private void  LoadListView(string strSQL)
        {
            if (strSQL != "")
            {
                frmListView frm = new frmListView();
                frm.Text = "Thông tin";
                frm.LoadForm(strSQL, txtConn);
                textBox.Text = frm.strSelectedItemData;
                txtCode = frm.strSelectedItemCode;
            }
        }


        public string strFieldname = "";
        public string fcFieldName
        {
            get
            {
                return strFieldname;
            }
            set
            {
                strFieldname = value;
            }
        }
        public string strParent = "";
        public string fcParentTable
        {
            get
            {
                return strParent;
            }
            set
            {
                strParent = value;
            }
        }





        private void textBox_DoubleClick(object sender, EventArgs e)
        {
            if (fcDate==true)
            {
                dateTimePicker.Visible = true;
                dateTimePicker.Focus();
                
            }
            else if (this.fcProc_ListView != "")
            {
                LoadListView("exec " + this.fcProc_ListView);

            }
            OnDoubleClick();
            this.Parent.Focus();
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == System.Windows.Forms.Keys.Enter && this.fcProc_ListView != "")
            {

                LoadListView("exec " + this.fcProc_ListView);
                OnDoubleClick();
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Tab)
            {
                if (textBox.Text == "" && (booReadOnly == true || booGenCode == true) && strProc_ListView != "")
                {
                    LoadListView("exec " + this.fcProc_ListView);
                }
            }
            else if(e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                OnKeyDown();
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (booDate == true && textBox.Text=="" && textBox.Text==" ")
            textBox.Text = System.DateTime.Now.ToString("MM/dd/yyyy");
            fcText = textBox.Text;
        }
        private bool booDate = false;
        public bool fcDate
        {
            get
            {
                return booDate;
            }
            set
            {
                booDate = value;
                if (booDate == true)
                {
                    dateTimePicker.Visible = true;
                    textBox.ReadOnly = true;
                    textBox.BackColor = System.Drawing.Color.LightBlue;
                }
            }
        }
        public bool booGetIt = true;
        public bool fcGetIt
        {
            get
            {
                return booGetIt;
            }
            set
            {
                booGetIt = value;
            }
        }

        public event EventHandler KeyDown;
        protected virtual void OnKeyDown()
        {
            if (this.KeyDown != null)
            {
                this.KeyDown(this, EventArgs.Empty);

            }
        }
        public enum DataType { Text, Bool, Numberic,Date };

        private DataType dType = DataType.Text;

        public DataType fcDataType
        {
            get
            {
                return dType;
            }
            set
            {
                this.dType = (DataType)value;
            }
        }

        private void dateTimePicker_Leave(object sender, EventArgs e)
        {
            fcText = dateTimePicker.Value.ToString("MM/dd/yyyy"); ;
           // dateTimePicker.Visible = false;
        }
        private char strEmpty;
        private string strPass = " ";
        public string fcPass
        {
            get
            {
                return strPass;
            }
            set
            {
                strPass = value;
                if (strPass != "" && strPass !=" ")
                {
                    textBox.PasswordChar =Convert.ToChar( strPass);
                }
                else
                    textBox.PasswordChar=Convert.ToChar(strEmpty );
            }
        }

        private bool booValidate = false;
        public bool fcValidate
        {
            get
            {
                return booValidate;
            }
            set
            {
                booValidate = value;
            }
        }

        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            fcText = dateTimePicker.Value.ToString("MM/dd/yyyy");
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (fcDataType==DataType.Numberic)
                if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
        }

        public DateTime fcMinDate
        {
            get
            {
                return dateTimePicker.MinDate;
            }
            set
            {
                dateTimePicker.MinDate = value;
            }
        }

        
        
    }
}
